package com.ruge.email;

import com.ruge.file.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.*;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;

/**
 * 爱丽丝、如歌  创建于 2018/10/20 14:04
 * 说明:  邮件工具类
 */
public class EmailUtil {
	public static Boolean sendSimpleEmail(EmailModel model) {
		Boolean result = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = null;
			Email email = null;
			String path = Thread.currentThread().getContextClassLoader().getResource("email.properties").getPath();
			in = new FileInputStream(path);
			prop.load(in);     ///加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			email = new SimpleEmail();
			while (it.hasNext()) {
				String key = it.next();
				System.out.println(key + ":" + prop.getProperty(key));
				if (StringUtils.equals(key, "email.hostName")) {
					email.setHostName(prop.getProperty(key));
				}
				if (StringUtils.equals(key, "email.port")) {
					email.setSmtpPort(Integer.parseInt(prop.getProperty(key)));
				}
				if (StringUtils.equals(key, "email.SSL")) {
					email.setSSL(Boolean.parseBoolean(prop.getProperty(key)));
				}
			}
			email.setAuthenticator(new DefaultAuthenticator(model.getUserName(), model.getPassWord()));
			email.setFrom(model.getUserName());
			email.setSubject(model.getTitle());
			email.setMsg(model.getText());
			for (String to : model.getToUser()
			) {
				email.addTo(to);
			}
			email.send();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static Boolean sendEmailWithAttachment(EmailModel model) {
		Boolean result = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = null;
			MultiPartEmail email = null;

			EmailAttachment attachment = new EmailAttachment();
			for (String att : model.getAttach()
			) {
				attachment.setPath(att);
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				//attachment.setDescription("约翰的图片");
				attachment.setName(FileUtil.getFileNameWithExpendName(att));
			}

			String path = Thread.currentThread().getContextClassLoader().getResource("email.properties").getPath();
			in = new FileInputStream(path);
			prop.load(in);     ///加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			email = new MultiPartEmail();
			while (it.hasNext()) {
				String key = it.next();
				System.out.println(key + ":" + prop.getProperty(key));
				if (StringUtils.equals(key, "email.hostName")) {
					email.setHostName(prop.getProperty(key));
				}
				if (StringUtils.equals(key, "email.port")) {
					email.setSmtpPort(Integer.parseInt(prop.getProperty(key)));
				}
				if (StringUtils.equals(key, "email.SSL")) {
					email.setSSL(Boolean.parseBoolean(prop.getProperty(key)));
				}
			}
			email.setAuthenticator(new DefaultAuthenticator(model.getUserName(), model.getPassWord()));
			email.setFrom(model.getUserName());
			email.setSubject(model.getTitle());
			email.setMsg(model.getText());
			for (String to : model.getToUser()
			) {
				email.addTo(to);
			}
			email.attach(attachment);
			email.send();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static Boolean sendEmailWithHtml(EmailModel model) {
		Boolean result = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = null;
			HtmlEmail email = null;
			String path = Thread.currentThread().getContextClassLoader().getResource("email.properties").getPath();
			in = new FileInputStream(path);
			prop.load(in);     ///加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			email = new HtmlEmail();
			while (it.hasNext()) {
				String key = it.next();
				System.out.println(key + ":" + prop.getProperty(key));
				if (StringUtils.equals(key, "email.hostName")) {
					email.setHostName(prop.getProperty(key));
				}
				if (StringUtils.equals(key, "email.port")) {
					email.setSmtpPort(Integer.parseInt(prop.getProperty(key)));
				}
				if (StringUtils.equals(key, "email.SSL")) {
					email.setSSL(Boolean.parseBoolean(prop.getProperty(key)));
				}
			}
			email.setAuthenticator(new DefaultAuthenticator(model.getUserName(), model.getPassWord()));
			email.setFrom(model.getUserName());
			email.setSubject(model.getTitle());
			email.setHtmlMsg(model.getText());
			for (String to : model.getToUser()
			) {
				email.addTo(to);
			}
			email.send();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static Boolean sendEmail(EmailModel model) {
		Boolean result = false;
		try {
			Properties prop = new Properties();
			FileInputStream in = null;
			HtmlEmail email = null;
			EmailAttachment attachment = new EmailAttachment();
			String path = Thread.currentThread().getContextClassLoader().getResource("email.properties").getPath();
			in = new FileInputStream(path);
			prop.load(in);     ///加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			email = new HtmlEmail();
			while (it.hasNext()) {
				String key = it.next();
				System.out.println(key + ":" + prop.getProperty(key));
				if (StringUtils.equals(key, "email.hostName")) {
					email.setHostName(prop.getProperty(key));
				}
				if (StringUtils.equals(key, "email.port")) {
					email.setSmtpPort(Integer.parseInt(prop.getProperty(key)));
				}
				if (StringUtils.equals(key, "email.SSL")) {
					email.setSSL(Boolean.parseBoolean(prop.getProperty(key)));
				}
			}
			email.setAuthenticator(new DefaultAuthenticator(model.getUserName(), model.getPassWord()));
			email.setFrom(model.getUserName());
			email.setSubject(model.getTitle());
			email.setHtmlMsg(model.getText());
			for (String to : model.getToUser()
			) {
				email.addTo(to);
			}
			for (String att : model.getAttach()
			) {
				attachment.setPath(att);
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				attachment.setName(FileUtil.getFileNameWithExpendName(att));
			}
			email.attach(attachment);
			email.send();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
